package com.dom.invoapp.modular.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.dom.invoapp.dao.entity.TOrganInfo;
import com.dom.invoapp.dao.entity.TStorInfo;
import com.dom.invoapp.dao.entity.TSuppInfo;
import com.dom.invoapp.dao.entity.TUserInfo;
import com.dom.invoapp.dao.service.ITOrganInfoService;
import com.dom.invoapp.dao.service.ITStorInfoService;
import com.dom.invoapp.dao.service.ITSuppInfoService;
import com.dom.invoapp.dao.service.ITUserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description 公共service服务
 * @Author xllei
 * @Date 2018/12/17 7:44 PM
 * @Version 1.0
 **/
@Service
public class CommonService {
    @Autowired
    private ITStorInfoService storInfoService;
    @Autowired
    private ITSuppInfoService suppInfoService;
    @Autowired
    private ITOrganInfoService organInfoService;
    @Autowired
    private ITUserInfoService userInfoService;

    /**
     * 获取仓库列表
     * @param memCode
     * @return
     */
    public Map<Integer, String> getStorList(int memCode) {
        List<TStorInfo> storInfos = getStorItems(memCode);
        Map<Integer, String> mapStor = storInfos.stream().collect(Collectors.toMap(TStorInfo::getId, entity -> {
            return entity.getStorAbbr();
        }));
        return mapStor;
    }

    /**
     * 获取仓库集合
     * @param memCode
     * @return
     */
    public List<TStorInfo> getStorItems(int memCode) {
        return storInfoService.selectInfoList(memCode);
    }

    /**
     * 获取机构列表
     * @param memCode
     * @return
     */
    public Map<Integer, String> getOrganList(int memCode) {
        List<TOrganInfo> organInfos = getOrganItems(memCode);
        Map<Integer, String> organList = organInfos.stream().collect(Collectors.toMap(TOrganInfo::getId, entity->{
            return entity.getOrganName();
        }));
        return organList;
    }

    /**
     * 获取机构集合
     * @param memCode
     * @return
     */
    public List<TOrganInfo> getOrganItems(int memCode) {
        Wrapper<TOrganInfo> wrapper = new EntityWrapper<>();
        wrapper.eq("mem_code", memCode)
                .orderBy("id", true);
        return organInfoService.selectList(wrapper);
    }

    /**
     * 获取供货商列表
     * @param memCode
     * @return
     */
    public Map<Integer, String> getSuppList(int memCode){
        List<TSuppInfo> suppInfos = getSuppItems(memCode);
        Map<Integer, String> suppList = suppInfos.stream().collect(Collectors.toMap(TSuppInfo::getSuppCode, entity->{
            return entity.getAbbrName();
        }));
        return suppList;
    }

    /**
     * 获取供货商集合信息
     * @param memCode
     * @return
     */
    public List<TSuppInfo> getSuppItems(int memCode){
        return suppInfoService.selectInfoList(memCode);
    }

    /**
     * 获取用户列表
     * @param memCode
     * @return
     */
    public Map<Integer, String> getUserList(int memCode){
        List<TUserInfo> userInfos = getUserItems(memCode);
        Map<Integer, String> userList = userInfos.stream().collect(Collectors.toMap(TUserInfo::getId, entity->{
            return entity.getUserName();
        }));
        return userList;
    }

    /**
     * 获取用户集合
     * @param memCode
     * @return
     */
    public List<TUserInfo> getUserItems(int memCode) {
        Wrapper<TUserInfo> wrapper = new EntityWrapper<>();
        wrapper.eq("mem_code", memCode)
                .orderBy("id", true);
        return userInfoService.selectList(wrapper);
    }
}
